/*
 * CreateGamePanel.java
 *
 * Created on September 28, 2008, 1:10 PM
 */
package no.eirikb.bomberman.applet.menu;

import no.eirikb.bomberman.applet.Bomberman;
import no.eirikb.bomberman.shared.Game;

/**
 *
 * @author  eirikb
 */
public class CreateGamePanel extends javax.swing.JPanel {

    private Bomberman bomberman;

    /** Creates new form CreateGamePanel */
    public CreateGamePanel(Bomberman bomberman) {
        this.bomberman = bomberman;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        createGameTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        createGameMaxPlayers = new javax.swing.JComboBox();
        createGameButton = new javax.swing.JButton();
        createGameErrorLabel = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        createGameShowNicks = new javax.swing.JComboBox();

        setBackground(java.awt.Color.red);

        jLabel2.setFont(new java.awt.Font("Dialog", 1, 14));
        jLabel2.setText("Game name:"); // NOI18N

        jLabel3.setFont(new java.awt.Font("Dialog", 1, 14));
        jLabel3.setText("Max players:"); // NOI18N

        createGameMaxPlayers.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2", "3", "4" }));

        createGameButton.setText("CREATE!"); // NOI18N
        createGameButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createGameButtonActionPerformed(evt);
            }
        });

        createGameErrorLabel.setFont(new java.awt.Font("Dialog", 1, 14));
        createGameErrorLabel.setText(" "); // NOI18N

        jLabel4.setFont(new java.awt.Font("Dialog", 1, 14));
        jLabel4.setText("Show nicks:"); // NOI18N

        createGameShowNicks.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Yes", "No" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 635, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(createGameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(createGameMaxPlayers, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(createGameButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(createGameErrorLabel)))
                        .addContainerGap(504, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(539, 539, 539))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(createGameShowNicks, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(564, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(createGameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(createGameMaxPlayers, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(createGameShowNicks, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 76, Short.MAX_VALUE)
                        .addComponent(createGameButton))
                    .addComponent(createGameErrorLabel))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

private void createGameButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createGameButtonActionPerformed
    if (createGameTextField.getText().length() > 0) {
        try {
            Game game = new Game(createGameTextField.getText(),
                    Integer.parseInt((String) createGameMaxPlayers.getSelectedItem()),
                    16,
                    16);
            game.setShowNicks(Boolean.valueOf((String) createGameShowNicks.getSelectedItem()));
            if (bomberman.getServer().createGame(bomberman.getHash(), bomberman.getUser().getNick(), game)) {
                game.addUser(bomberman.getUser());
                bomberman.getUser().setGameName(game.getName());
                bomberman.showGameWaitPanel(game);
            } else {
                createGameErrorLabel.setText("Error: Game already exists");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        createGameErrorLabel.setText("Error: Please enter a name for the game");
    }
}//GEN-LAST:event_createGameButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton createGameButton;
    private javax.swing.JLabel createGameErrorLabel;
    private javax.swing.JComboBox createGameMaxPlayers;
    private javax.swing.JComboBox createGameShowNicks;
    private javax.swing.JTextField createGameTextField;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    // End of variables declaration//GEN-END:variables
}
